﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DiagramDesigner.BusinessModelObject;

namespace DiagramDesigner
{
    /// <summary>
    /// Interaction logic for CustomCommandUse.xaml
    /// </summary>
    public partial class GUIFactTable 
    {
        public GUIFactTable()
        {
            InitializeComponent();
            lstview.ItemsSource = Utils.MeasureAttribute.StockCheck();
            lstview.SelectionChanged += ItemSelectionChanged;
            lstview.KeyUp += new KeyEventHandler(lstview_KeyUp);
            //foreach (Utils.MeasureAttribute gr in Utils.MeasureAttribute.StockCheck())
            //{
            //    lstview.Items.Add(gr);
            //    lstview.FontStyle = FontStyles.Italic;
            //}
        }

        void lstview_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Subtract)
            {
                //MessageBox.Show("Evento");
                eliminarFila();
                OrganizarColumnas(false);
            }
            //MessageBox.Show(e.Key.ToString());
            //throw new NotImplementedException();
        }    

        
        private void OnAddListItemExecute(object sender, ExecutedRoutedEventArgs e)
        {
            ListView lv = sender as ListView;
            if (lv != null)
            {
                lv.Items.Add(e.Parameter);
                //txtItem.Clear();
                e.Handled = true;
            }
        }

        private void OnRemoveListItemExecute(object sender, ExecutedRoutedEventArgs e)
        {
            ListView lv = sender as ListView;
            if (lv != null)
            {
                if (lv.SelectedItem != null)
                {
                    lv.Items.Remove(lv.SelectedItem);
                    e.Handled = true;
                }
            }
        }

        private void CanAddListItemExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;// !String.IsNullOrEmpty(txtItem.Text) && !listview.Items.Contains(txtItem.Text);
            e.Handled = true;
        }

        private void CanRemoveListItemExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            ListView lv = sender as ListView;
            if (lv != null)
            {
                e.CanExecute = lv.SelectedItem != null;
                e.Handled = true;
            }
        }

        public void eliminarFila()
        {
            if (lstview.SelectedItem != null)
            {
                //lv.Items.Remove(lv.SelectedItem);
                List<Utils.MeasureAttribute> CurrentProducts = (List<Utils.MeasureAttribute>)lstview.ItemsSource;
                List<Utils.MeasureAttribute> NewProducts = new List<DiagramDesigner.Utils.MeasureAttribute>();
                foreach (Utils.MeasureAttribute dr in CurrentProducts)
                {
                    if (dr != lstview.SelectedItem)                    
                    {
                        NewProducts.Add(dr);
                    }
                }

                //Atributos
                DesignerItem dItem = (DesignerItem)((GUIFactTable)((DockPanel)((Grid)((Border)((Grid)((StackPanel)lstview.Parent).Parent).Parent).Parent).Parent).Parent).Parent;
                MainElementModel onk = (MainElementModel)dItem.Item;
                Dictionary<int, BaseField> _atributos = onk.Atributos;

                Dictionary<int, BaseField> _atributosFinal = new Dictionary<int, BaseField>();

                int contador = 0;

                int intInicio = 0;
                int intFinal = _atributos.Count;

                if (_atributos.ContainsKey(-1))
                {
                    intInicio = -1;
                    intFinal = _atributos.Count - 1;
                }

                for (int k = intInicio; k < intFinal; k++)
                {
                    if (k != lstview.SelectedIndex)
                    {
                        _atributosFinal[contador] = _atributos[k];
                        contador = contador + 1;
                    }
                }

                onk.Atributos.Clear();

                intInicio = 0;
                intFinal = _atributosFinal.Count;

                if (_atributosFinal.ContainsKey(-1))
                {
                    intInicio = -1;
                    intFinal = _atributosFinal.Count - 1;
                }
                for (int k = intInicio; k < intFinal; k++)
                {
                    onk.Atributos.Add(k, _atributosFinal[k]);
                }

                lstview.ItemsSource = NewProducts;
                lstview.Items.Refresh();
            } 
        }

        public void OrganizarColumnas(bool modifyItem)
        {
            GridView gv = lstview.View as GridView;
            bool oneColumn = true;

            List<Utils.MeasureAttribute> CurrentProducts = (List<Utils.MeasureAttribute>)lstview.ItemsSource;
            foreach (Utils.MeasureAttribute dr in CurrentProducts)
            {
                if (dr.RutaImagen != "Resources/Images/blank.png")
                {
                    oneColumn = false;
                }
            }

            if (oneColumn)
            {
                gv.Columns[0].Width = lstview.Width;
                gv.Columns[1].Width = 0;
                gv.Columns[2].Width = 0;
            }
            else
            {
                if (modifyItem)
                {
                    gv.Columns[0].Width = lstview.Width;
                    gv.Columns[1].Width = 34;
                    gv.Columns[2].Width = 36;

                    //((GUIFactTable)((DockPanel)grvFactTable.Parent).Parent).Width = ((GUIFactTable)((DockPanel)grvFactTable.Parent).Parent).ActualWidth + 70;

                    //((DockPanel)grvFactTable.Parent).Width = ((DockPanel)grvFactTable.Parent).RenderSize.Width + 70;
                    ((DesignerItem)((GUIFactTable)((DockPanel)grvFactTable.Parent).Parent).Parent).Width = ((DesignerItem)((GUIFactTable)((DockPanel)grvFactTable.Parent).Parent).Parent).Width + 70;

                    //grvFactTable.Width = grvFactTable.Width + 70;
                    //lstview.Width = lstview.Width + 70;
                    //maskFactTable.Width = maskFactTable.Width + 70;
                    //maskMainFactTable.Width = maskMainFactTable.Width + 70;
                                     
                    
                }
                else
                {
                    if (lstview.Width - 70 > 0)
                    {
                        gv.Columns[0].Width = lstview.Width - 70;
                    }
                    else
                    {
                        gv.Columns[0].Width = 0;
                    }
                    gv.Columns[1].Width = 34;
                    gv.Columns[2].Width = 36;
                }
            }
        }

        private void lstview_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            OrganizarColumnas(false);
        }

        private void Border_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            grView.Height = maskMainFactTable.Height  ;
            if (maskMainFactTable.Width - 15 > 0)
                grView.Width = maskMainFactTable.Width - 15;
            else
                grView.Width = 0;


            maskFactTable.Height = maskMainFactTable.Height;
            maskFactTable.Width = maskMainFactTable.Width;

            lstview.Height = maskMainFactTable.Height;
            lstview.Width = maskMainFactTable.Width; 
        }

        private void Grid_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            Grid om = sender as Grid;
            double alto = ((DockPanel)om.Parent).RenderSize.Height;
            double ancho = ((DockPanel)om.Parent).RenderSize.Width;

            maskMainFactTable.Width = ancho;
            if (alto - 86 > 0)
                maskMainFactTable.Height = alto - 86;
            else
                maskMainFactTable.Height = 0;
            
        }
    }

}
